package org.corey.share.misc;

import java.util.HashMap;
import java.util.Map;

import org.corey.share.db.entity.Article;
import org.corey.share.db.service.ArticleService;
import org.corey.share.db.service.ArticleServiceImpl;

public class SystemContext {
	private ArticleService articleService = new ArticleServiceImpl();

	private Map<Long, Integer> articlesReadCountCache = new HashMap<Long, Integer>();
	private static SystemContext context = new SystemContext();;

	private SystemContext() {
	}

	public static SystemContext getContext() {
		return context;
	}

	public int increaseReadCount(Article a) {
		Integer count = articlesReadCountCache.get(a.getId());
		if (count == null) {
			count = a.getReadCount() + 1;
		} else {
			count++;
		}
		articlesReadCountCache.put(a.getId(), count);
		if (count % 5 == 0) {
			a.setReadCount(count);
			articleService.modifyArticle(a);
		}
		return count;
	}
	
	public int getReadCount(Article a,int readCount){
		Integer count = articlesReadCountCache.get(a.getId());
		if(count==null){
			articlesReadCountCache.put(a.getId(),readCount);
		}
		return articlesReadCountCache.get(a.getId());
	}
}
